package com.xenstra.bhavcopydownloader;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DailyLoader {

	public static void main(String[] args) throws Exception {
		if(args.length == 0){
			System.out.println("Download folder name must be provided !");
			System.exit(1);
		}

		// Check if download folder exists or not. MUST pre-exist.
		File downloadLocation = new File(args[0]);
		if(! downloadLocation.exists() || ! downloadLocation.isDirectory()){
			System.out.print("Download folder doesn't exist.");
			System.exit(0);
		}

		// start download
		Calendar startDate = Calendar.getInstance();
		new Downloader(args[0], startDate).downloadSingleDayFiles();	

		startDate.set(Calendar.HOUR_OF_DAY, 0);
		startDate.set(Calendar.MINUTE, 0);
		startDate.set(Calendar.SECOND, 0);
		startDate.set(Calendar.MILLISECOND, 0);

		Date today = startDate.getTime();


		String folder = args[0] + File.separator +  startDate.get(Calendar.YEAR);
		List<File> files = Utils.getAllFilesInFolder(folder, today, today);
		System.out.println("\n" + files.size() + " files in " + folder);

		BhavCopyDBUploader uploader = new BhavCopyDBUploader();
		uploader.init();
		
		for(File file : files){
			uploader.uploadToDB(file, true, true);
		}			
	}

}
